﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace FundamentalsOfCompiling
{
    public class SourceFile:ISource
    {
        private string lineBuf = null;//一行字符串
        private int length;//buffer的长度
        private string path;
        private int linepos = 0;//字符串的指针，标识处理位置
        private StreamReader source = null;
        private bool EOF_flag;
        public SourceFile(string path) {
            this.path = path;
            length = 0;
            linepos = 0;
            EOF_flag = false;
        }
     int ISource.getNextChar(){
         if(source==null)
             source = new StreamReader(this.path);
         if (!(linepos < length)) {
                //global.operatelineno = global.operatelineno + 1;
                //if (global.operateTokentnum > 0)
                //{
                //    TokenType tokenline = new TokenType();
                //    tokenline.lineshow = 0;
                //    tokenline.Lex = LexType.CRT;//存储一个换行符号
                //    tokenline.Sem = "\n";
                //    list.Add(tokenline);
                //    global.operateTokentnum = global.operateTokentnum + 1;
                //}

                if ((lineBuf = source.ReadLine()) != null)
                {
                    lineBuf = "   " + lineBuf+'\n';
                    length = lineBuf.Length;
                    linepos = 0;
                    return lineBuf[linepos++];
                }
                else
                {
                    EOF_flag = true;
                    return -1;
                }
            }
            else return lineBuf[linepos++];    
        }
     void ISource.ungetNextChar(){
           if (!EOF_flag)
              linepos--;
        }
     void ISource.close() {
            if (source != null)
                source.Close();
        }
    }
}
